<?php

class PresentationHelper extends Helper {

    public $helpers = array('Form');
    private $view = null;

    public function __construct(View $view, $settings = array()) {
        parent::__construct($view, $settings);
        $this->view = $view;
    }

    public function getContent($method, $settings) {
        global $MODEL;
        $array = array();
        $settings = $this->parseSettings($settings);
        $array["label"] = $settings["label"];
        if (empty($settings["initialvalue"])) {
            $array["empty"] = "";
        }
        $class = isset($settings["presentation"]) ? strtolower($settings["presentation"]) : null;

        $settings['field'] = $settings['name'];
        if (isset($this->view)) {
            $settings["view"] = $this->view;
            $cakeInfo = $this->view->Form->_initInputField(strpos($settings['field'], ".") !== false ? $settings['field'] : $MODEL->name . '.' . $settings['field']);
            $settings['name'] = $cakeInfo['name'];
            $settings['id'] = $cakeInfo['id'];
        }

        // Presentation en path?
        $path = "";
        if (strpos($class, "::") !== false) {
            list($path, $class) = explode("::", $class);
        }
        
        // Existe presentation?
        if ($url = existePresentation($class, $path)) {
            include_once($url);
            $class = "PST_" . $class;
            if (class_exists($class)) {
                $obj = new $class($settings);
            } else {
                include_once(ROOT . '/app/Presentation/default/text.php');
                $obj = new PST_TEXT($settings);
            }
        } else {
            include_once(ROOT . '/app/Presentation/default/text.php');
            $obj = new PST_TEXT($settings);
        }

        // Si tiene alguna inclusion de JS las hago
        $js_includes = $obj->jsIncludes();
        if (is_array($js_includes) && !empty($js_includes)) {
            foreach ($js_includes as $js_include) {
                $this->view->Html->script($js_include, array('inline' => false));
            }
        }

        // Si tiene un js_initial lo agrega
        if (isset($obj->js_initial)) {
            $params = array();
            if (isset($obj->js_params) && is_array($obj->js_params)) {
                $params = array_merge($params, $obj->js_params);
            }
            if (isset($obj->classparams)) {
                $params = array_merge($params, $obj->classparams);
            }

            $field = '$("#' . $cakeInfo['id'] . '")';
            $params = (is_array($params) ? json_encode($params) : '');
            $this->view->Html->scriptBlock("$(document).ready(function() { " . $obj->js_initial . "(" . $field . ", $.parseJSON('" . $params . "')); });", array('inline' => false));
        }

        // Si recibo un array es un Cake Form Object
        if (is_array($content = $obj->$method())) {
            return $this->view->Form->input($settings['field'], array_merge($content, $array));
        }
        return $content;
    }

    private function parseSettings($settings) {
        foreach ($settings["data"] as $key => $value) {
            if (empty($settings[$key])) {
                $settings[$key] = $value;
            }
        }
        unset($settings["data"]);
        return $settings;
    }

}
